/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.ui.text;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.eclipse.dltk.ui.text.AbstractScriptScanner;
import org.eclipse.dltk.ui.text.IColorManager;
import org.eclipse.dltk.ui.text.rules.ScriptWordRule;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordPatternRule;

import com.funkyroach.use.core.language.USKeywords;
import com.funkyroach.use.ui.text.rules.USFloatNumberRule;
import com.funkyroach.use.ui.text.rules.USMacrosDetector;
import com.funkyroach.use.ui.text.rules.USWordDetector;

public class USCodeScanner extends AbstractScriptScanner {

    // public static final String[] KEYWORDS2 = { "abstract", "config",
    // "nousercreate", "native", "nativereplication",
    // "safereplace", "within", "perobjectconfig", "transient", "noexport",
    // "dependson", "exportstructs",
    // "cacheexempt", "hidedropdown", "parseconfig", "collapsecategories",
    // "dontcollapsecategories",
    // "hidecategories", "showcategories", "placeable", "notplaceable",
    // "editinlinenew", "noteditinlinenew",
    // "instanced", "function", "event", "delegate", "final", "iterator",
    // "latent", "native", "intrinsic",
    // "simulated", "singular", "static", "exec", "protected", "private", "out",
    // "optional", "coerce", "skip",
    // "defaultproperties", "replication", "reliable", "unreliable", "new" };

    // public static final String[] KEYWORDS3 = { "struct", "class", "state",
    // "extends", "var", "const", "local",
    // "config", "const", "deprecated", "edfindable", "editconst",
    // "editconstarray", "editinline", "export",
    // "noexport", "globalconfig", "input", "localized", "native", "private",
    // "protected", "transient", "travel",
    // "byte", "bool", "int", "string", "enum", "name", "float", "array",
    // "guid", "Super", "EnumCount" };

    // public static final String[] KEYWORDS4 = { "return", "break", "continue",
    // "stop", "assert", "goto", "if", "else",
    // "switch", "case", "default", "while", "for", "do", "until", "foreach",
    // "cross", "dot", "clockwisefrom" };

    private static String s_TokenProperties[] = new String[] { USColorConstants.US_KEYWORD, USColorConstants.US_NUMBER,
            USColorConstants.US_CLASS, USColorConstants.US_COMMENT, USColorConstants.US_MACROS,
            USColorConstants.US_DEFAULT, };

    public USCodeScanner(IColorManager manager, IPreferenceStore store) {
        super(manager, store);
        initialize();
    }

    @Override
    protected List<IRule> createRules() {
        List<IRule> rules = new ArrayList<IRule>();
        IToken keywordToken = getToken(USColorConstants.US_KEYWORD);
        IToken number = getToken(USColorConstants.US_NUMBER);
        IToken classToken = getToken(USColorConstants.US_CLASS);
        IToken comment = getToken(USColorConstants.US_COMMENT);
        IToken macros = getToken(USColorConstants.US_MACROS);
        IToken other = getToken(USColorConstants.US_DEFAULT);

        // Add rule for single line comments.
        rules.add(new EndOfLineRule("//", comment));
        rules.add(new ScriptWordRule(new USMacrosDetector(), macros));
        // Add generic whitespace rule.
        rules.add(new WhitespaceRule(new USWhitespaceDetector()));

        ScriptWordRule wordRule = new ScriptWordRule(new USWordDetector(), other, true);

        wordRule.addNextTokenAfterSeen("class", classToken);
        wordRule.addNextTokenAfterSeen("extends", classToken);

        Set<String> keywords = USKeywords.instance().getAllKeywords();
        for (String keyword : keywords) {
            wordRule.addWord(keyword, keywordToken);
        }

        rules.add(new USFloatNumberRule(number));
        rules.add(wordRule);

        setDefaultReturnToken(other);

        return rules;
    }

    @Override
    protected String[] getTokenProperties() {
        return s_TokenProperties;
    }

}
